package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.CollectionEntity;
import org.example.system.vo.BookmarkVo;
import org.example.system.mapper.BookmarkMapper;
import org.example.system.service.BookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 收藏服务实现
 */
@Service
public class BookmarkServiceImpl implements BookmarkService {
    @Autowired
    private BookmarkMapper bookmarkMapper;

    /**
     * 按条件分页查询
     * @param bookmark
     * @return
     */
    @Override
    public IPage<BookmarkVo> selectPage(BookmarkVo bookmark){
        MPJLambdaWrapper<BookmarkVo> wrapper = new MPJLambdaWrapper<>();
        if (bookmark.getCollectionId() != null){
            wrapper.eq(BookmarkVo:: getCollectionId,bookmark.getCollectionId());
        }
        if (bookmark.getUserId() != null){
            wrapper.eq(BookmarkVo:: getUserId,bookmark.getUserId());
        }
        if (bookmark.getMediaType() != null){
            wrapper.eq(CollectionEntity:: getMediaType,bookmark.getMediaType());
        }
        wrapper.selectAll(BookmarkVo.class);
        wrapper.leftJoin(CollectionEntity.class, CollectionEntity::getId, BookmarkVo::getCollectionId);
        wrapper.selectAs(CollectionEntity::getCoverPath, "cover_path");
        wrapper.selectAs(CollectionEntity::getMediaTitle, "media_title");
        wrapper.selectAs(CollectionEntity::getMediaType, "media_type");
        IPage<BookmarkVo> page = new Page<>();
        page.setCurrent(bookmark.getPageNum());
        page.setSize(bookmark.getPageSize());
        return bookmarkMapper.selectPage(page,wrapper);
    }
    /**
     * 添加收藏
     * @param bookmark
     * @return
     */
    @Override
    public int add(BookmarkVo bookmark) {
        return bookmarkMapper.insert(bookmark);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return bookmarkMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
